package com.panopset.db;

import java.io.StringWriter;

public abstract class DataField {
	public DataField(final String name) {
		nm = name;
	}

	public String getName() {
		return nm;
	}

	public abstract String getAttributes();

	/**
	 * Defaults to empty String.
	 * @return Additional attributes, if overridden.
	 */
	public String getAdditionalAttributes() {
		return "";
	}

	public boolean hasAdditionalAttributes() {
		return getAdditionalAttributes() != null
				&& getAdditionalAttributes().trim().length() > 0;
	}

	protected boolean isPrimaryKey() {
		return false;
	}

	public boolean isIndexed() {
		return isIndexed;
	}

	public DataField setIndexed(boolean value) {
		isIndexed = value;
		return this;
	}

	public boolean isUnique() {
		return isUnique;
	}

	public DataField setUnique(boolean value) {
		isUnique = value;
		return this;
	}

	private Boolean isIndexed = false;
	private Boolean isUnique = false;
	private final String nm;

	private String tblCmd;

	public String getCreateTableCmd() {
		if (tblCmd == null) {
			StringWriter sw = new StringWriter();
			sw.append(getName());
			sw.append(" ");
			sw.append(getAttributes());
			tblCmd = sw.toString();
		}
		return tblCmd;
	}
}
